<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Hyundai
 * COPYRIGHT	: 2008
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 * 
 * </pre>
 * 
 * Created on	:  Sep 24, 2008
 * 
 * @author		sanglt <sanglt@rubik.com.vn>
 * @access		[public/private]
 * @package		[package name]
 *
 */

defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RCoreImporter::cls( "html.HtmlControl" );

class FieldControl extends HtmlControl {
	
	var $_dtoArray;
	var $_languageArray;
	var $_languageDefault;
	
	var $_mutitab;
	
	var $_arrayListField;
	
	var $_jsValidate = true;
	
	/**
	 * Database 
	 *
	 * @var Database
	 */
	var $_db;
	
	var $_headercss;
	var $_headerscript;
	var $_header;
	
	/**
	 * Constructor
	 *
	 * @param array or dto $dtoArray
	 * @param array or string $defaultValue
	 * @param array $languageArray
	 * @param string $languageDefault
	 */
	function __construct($dtoArray, $arrayListField = null, $languageArray = '', $languageDefault = '') {
		$this->_db = &SysDatabase::getInstance ();
		$this->_dtoArray = $dtoArray;
		if ($languageArray == '') {
			$config = SystemSite::getSiteConfig ();
			$this->_languageArray = $config->language_array;
		} else {
			$this->_languageArray = $languageArray;
		}
		
		if ($languageDefault == '') {
			$lang = SystemSite::getLanguage ();
			$this->_languageDefault = $lang;
		} else {
			$this->_languageDefault = $languageDefault;
		}
		if (!$arrayListField) {
			$arrayListField = RubikHelper::getProperties($dtoArray);
			RubikHelper::debugPrePrint($arrayListField);
		}
		$this->_arrayListField = $arrayListField;
		$this->_prepaidData ();
	}
	function setExpectedArrange($arrExpectedArrange) {
		$this->_expectedArrange = $arrExpectedArrange;
		// Calculate the total column
		foreach ($arrExpectedArrange as $arrOrKey) {
			$num = 2;
			if (is_array($arrOrKey)) {
				$num = count($arrOrKey);
				$num = $num * 2;
			}
			$this->_totalCol = max($this->_totalCol, $num);
		}
	}
	/**
	 * Prepaid data
	 *
	 */
	function _prepaidData() {
		$dtoArray = $this->_dtoArray;
		if (! is_array ( $dtoArray )) {
			$this->_mutitab = false;
		} else {
			$this->_mutitab = true;
		}
		$this->_buildHeader ();
	}
	
	function _buildHeader() {
		$siteLink = SYS_TMPL_URL;
		$_headercss = '<style>
/* Style for orderlist */
ol.tab {
	background: url(' . $siteLink . 'dot.gif) repeat-x left bottom;
	list-style: none;
	margin: 0;
	padding: 6px 0;
	position: relative;
}

ol.tab li {
	background: #F2F5FA;
	border: 1px solid #D3DDED;
	display: inline;
	margin-right: 5px;
	padding: 0;
}
html > body ol.tab li {
	margin-right: 0px;
	padding: 5px 0;
}

ol.tab li.active {
	background: #FFF;
	border-bottom: 1px solid #FFF;
	font-weight: bold;
	padding: 5px 10px;
}

/* Style for link tag */
ol.tab a {
	font-weight: bold;
	margin: 0;
	padding: 5px 10px;
}

div#contentfield {
	border: 1px solid #D3DDED;
	border-top: none;
	padding: 10px;
}

table#contenttext {
	border-top: none;
	padding: 0px;
}
</style>';
		$this->_headercss = $_headercss;
		
		if ($this->_mutitab) {
			$headerscript = "<script type=\"text/javascript\">
								function changeTab(lang){
									var content = '#content'+lang;
									var item = '#item'+lang;
									$('#langtabmenu').children('li').removeClass('active');
									$('#contentfield').children('table').attr('style','display:none;');
									$(item).addClass('active');
									$(content).attr('style','display:block;');
								}
								</script>";
			$langDefault = $this->_languageDefault;
			
			$langArray = $this->_languageArray;
			
			$header = '<ol class="tab" id="langtabmenu">';
			foreach ( $langArray as $lang ) {
				$viewlang = SysLanguage::_ ( $lang );
				if ($lang == $langDefault) {
					$header .= '<li id="item' . $lang . '" class="active"><a href="javascript:changeTab(\'' . $lang . '\');" title="' . $viewlang . '">' . $viewlang . '</a></li>' . "\n";
				} else {
					$header .= '<li id="item' . $lang . '"><a href="javascript:changeTab(\'' . $lang . '\');" title="' . $viewlang . '">' . $viewlang . '</a></li>' . "\n";
				}
			}
			$header .= "</ol>";
		} else {
			$header = '';
		}
		$this->_headerscript = $headerscript;
		$this->_header = $header;
	}
	
	/**
	 * Build Field
	 *
	 * @param  RubikDBField $dto
	 * @return unknown
	 */
	function _buildField($dto, $langname = '') {
		$dbInfo = RubikDBFactory::getDBInfo ( $dto );
		$dataFields = $dbInfo->getAllField ();
		$allFields = $this->_arrayListField;
		$return = '';
		if ($langname != '') {
			$prefix = 'mu_' . $langname;
		} else {
			$prefix = '';
		}
		foreach ( $allFields as $key => $fieldRule ) {
			$fieldInfo = $dbInfo->getDbField ( $key );
			$lbl_field = SysLanguage::_ ( $key );
			$fieldName = $prefix . $key;
			$fieldData = $dto->$key;
			
			// Check if Key
			if ($fieldInfo->isKey) {
				$fieldData = htmlspecialchars($fieldData);
				$return .= "<input type=\"hidden\" id=\"{$fieldName}\" name=\"{$fieldName}\" value=\"{$fieldData}\" />";
				// Check if number
			} else {
				$requireSym = '';
				$requireCls = null;
				if ($fieldInfo->isMandatory) {
					$requireSym = ' <span class="required">*</span>';
					$requireCls = 'required';
				}
				if (!($fieldRule->_isColumnInfo || $fieldRule->isHtmlFieldInfo)) {
					if ($fieldInfo && $fieldInfo->isBoolean()) {
						RCoreImporter::cls('html.fieldInfo.CheckboxHtmlFieldInfo');
						$fieldRule = new CheckboxHtmlFieldInfo($fieldName, $fieldRule);
					}
					else {
						RCoreImporter::cls('html.fieldInfo.BasicHtmlFieldInfo');
						$fieldRule = new BasicHtmlFieldInfo($fieldName, $fieldRule);
					}
				}
				
				if ($fieldRule->_isColumnInfo) {
					$fieldRule->setPrefixToName ( $prefix );
					$fieldData = $fieldRule->generateHTML ( $fieldData );
					$return .= "<tr><td class=\"form-lable\"><label for=\"{$fieldName}\">{$lbl_field}{$requireSym}</label></td>	<td class=\"form-input\">{$fieldData}</td></tr>";
				}else if ($fieldRule->isHtmlFieldInfo) {
					$fieldRule->setValue($fieldData);
					$fieldRule->setValidateCssCls($requireCls, '_req');
					$fieldData = $fieldRule->getHtmlInputData($fieldInfo->dataType);
					$return .= "<tr><td class=\"form-lable\"><label for=\"{$fieldName}\">{$lbl_field}{$requireSym}</label></td>	<td class=\"form-input\">{$fieldData}</td></tr>";
				} else {
					$fieldData = htmlspecialchars($fieldData);
					$return .= "<tr><td class=\"form-lable\"><label for=\"{$fieldName}\">{$lbl_field}{$requireSym}</label></td>	<td class=\"form-input\"><input name=\"{$fieldName}\" value=\"{$fieldData}\" id=\"{$fieldName}\" /></td></tr>";
				}
			}
		}
		return $return;
	}
	
	function _buildData() {
		if ($this->_mutitab) {
			$return = "<div id=\"contentfield\">";
			foreach ( $this->_dtoArray as $langname => $dto ) {
				if ($langname == $this->_languageDefault) {
					$style = "display:block;";
				} else {
					$style = "display:none;";
				}
				$return .= "<table id=\"content" . $langname . "\" class=\"tblControll\" style=\"{$style}\">";
				$return .= $this->_buildField ( $dto, $langname );
				$return .= "</table>";
			}
			$return .= "</div>";
			return $return;
		} else {
			$return = "<table id=\"contenttext\" class=\"tblControll\">";
			$return .= $this->_buildField ( $this->_dtoArray );
			$return .= "</table>";
			return $return;
		}
	}
	
	/**
	 * Enabale or disable js database
	 *
	 * @param boolean $validate
	 */
	function setJsValidate($validate) {
		$this->_jsValidate = $validate;
	}
	
	/**
	 * Generate the HTML
	 *
	 * @return string
	 */
	function generateHtml() {
		if ($this->_jsValidate) {
			SysResponse::addJavascriptFile('core/js/jquery.validate.min.js');
			
			$js = '
				$(document).ready(function(){
					if ($.validator) {
						$("#systemForm").validate();
					}
				});
			';
			SysResponse::addJavascriptContent($js);
		}
		
		$result = '';
		$result .= $this->_headerscript;
		$result .= '<div class="wrapper">';
		$result .= $this->_header;
		$result .= $this->_buildData ();
		$result .= '</div>';
		return $result;
	}
	
	/**
	 * Print the html
	 *
	 */
	function printHtml() {
		static $headercss;
		if (! isset ( $headercss )) {
			$headercss = 'loaded';
			echo $this->_headercss;
		}
		echo $this->_headerscript;
		echo '<div class="wrapper">';
		echo $this->_header;
		echo $this->_buildData ();
		echo '</div>';
	}
}
?>